require "Dialogs"

Arena.Resize(130, 130)
timer = 0
tableaux = 0
auxang = 0

state = 0
newpX = Player.absx
newpY = 0

if Encounter["Glow"].alpha >= 0.9 then
	Danio = 3
elseif Encounter["Glow"].alpha >= 0.6 then
	Danio = 2
elseif Encounter["Glow"].alpha >= 0.3 then
	Danio = 1
end
Encounter.Call("AttackAnim", 1)
SetPPCollision(true)

Bones = {}
GBPaps = {}

function RecolorSoul()
	if state == 0 then
		Player.sprite.color = {1.0,0.0,0.0}
		Player.sprite.rotation = 0
	end
end

RecolorSoul()

function Update()
	DialogUpdate()
	if not StopWave then
		timer = timer + 1
		if timer == 1 then
			Bone = CreateProjectileAbs("Bullets/Bones/Long Bone", 326, 354)
			HandL = CreateProjectileAbs("Papyrus/Arms/Left Hand", 276, 348)
			HandR = CreateProjectileAbs("Papyrus/Arms/Right Hand", 371, 351)
			Bone.sprite.rotation = 4
			Bone.sprite.xscale = 1.2
			Bone.SetVar("MoreKR", 20)
			Bone.SetVar("MoreDamage", 10)
			Bone.SetVar("X", 326)
			HandR.SetVar("X", 371)
		elseif (timer >= 30 and timer < 40) or (timer >= 65 and timer < 75) then
			auxang = auxang + 1
			Bone.MoveToAbs(Bone.GetVar("X") + 16 - 16*math.cos(math.rad(18*auxang)), 354 + 16*math.sin(math.rad(18*auxang)))
			HandR.MoveToAbs(HandR.GetVar("X") + 16 - 16*math.cos(math.rad(18*auxang)), 351 + 16*math.sin(math.rad(18*auxang)))
			if timer == 39 or timer == 74 then
				Bone.SetVar("X", Bone.GetVar("X") + 32)
				HandR.SetVar("X", HandR.GetVar("X") + 32)
				auxang = 0
			end
		elseif timer >= 50 and timer < 55 then
			HandR.MoveToAbs(HandR.GetVar("X") - 32/5, 351)
			HandR.SetVar("X", HandR.GetVar("X") - 32/5)
		elseif timer == 85 then
			Encounter.Call("AttackAnim", 2)
			HandR.Remove()
			HandR = nil
		elseif timer == 105 then
			if math.random(2) == 1 then
				Bone.sprite.color = {33/255, 197/255, 1}
				Bone.SetVar("tipo", "Blue")
			else
				Bone.sprite.color = {1, 150/255, 0}
				Bone.SetVar("tipo", "Orange")
			end
		elseif timer == 115 then
			Bone.sprite.color = {1, 1, 1}
		elseif timer >= 135 and timer < 145 then
			Bone.sprite.rotation = 4 + 10*math.sin(math.rad(9*(timer - 134)))
			Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation)))
		elseif timer >= 145 and timer < 170 then
			if Bone.GetVar("tipo") == "Blue" then
				Bone.sprite.color = {33/255, 197/255, 1}
			elseif Bone.GetVar("tipo") == "Orange" then
				Bone.sprite.color = {1, 150/255, 0}
			end
			Bone.sprite.rotation = 14 - 120*math.sin(math.rad(90/25*(timer - 144)))
			Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation)))
		elseif timer == 170 then
			Bone.sprite.color = {1, 1, 1}
			if timerEnd then
				StopWave = true
			end
		elseif timer == 180 then
			if math.random(2) == 1 then
				Bone.sprite.color = {33/255, 197/255, 1}
				Bone.SetVar("tipo1", "Blue")
			else
				Bone.sprite.color = {1, 150/255, 0}
				Bone.SetVar("tipo1", "Orange")
			end
		elseif timer == 190 then
			Bone.sprite.color = {1, 1, 1}
		elseif timer == 200 then
			if math.random(2) == 1 then
				Bone.sprite.color = {33/255, 197/255, 1}
				Bone.SetVar("tipo2", "Blue")
			else
				Bone.sprite.color = {1, 150/255, 0}
				Bone.SetVar("tipo2", "Orange")
			end
		elseif timer == 210 then
			Bone.sprite.color = {1, 1, 1}
		elseif timer == 220 then
			if math.random(2) == 1 then
				Bone.sprite.color = {33/255, 197/255, 1}
				Bone.SetVar("tipo3", "Blue")
			else
				Bone.sprite.color = {1, 150/255, 0}
				Bone.SetVar("tipo3", "Orange")
			end
			if math.random(2) == 1 then
				Bone.SetVar("tipo4", "Blue")
			else
				Bone.SetVar("tipo4", "Orange")
			end
		elseif timer == 230 then
			Bone.sprite.color = {1, 1, 1}
		elseif timer >= 240 and timer < 250 then
			Bone.sprite.rotation = -106 - 10*math.sin(math.rad(9*(timer - 239)))
			Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation)))
		elseif timer >= 250 and timer < 350 then
			if timer == 250 then
				BoneEffect4 = CreateProjectileAbs("Bullets/Bones/Long Bone", Bone.absx, Bone.absy, "BelowBullet")
				BoneEffect3 = CreateProjectileAbs("Bullets/Bones/Long Bone", Bone.absx, Bone.absy, "BelowBullet")
				BoneEffect2 = CreateProjectileAbs("Bullets/Bones/Long Bone", Bone.absx, Bone.absy, "BelowBullet")
				BoneEffect1 = CreateProjectileAbs("Bullets/Bones/Long Bone", Bone.absx, Bone.absy, "BelowBullet")
				BoneEffect1.sprite.xscale = 1.2
				BoneEffect2.sprite.xscale = 1.2
				BoneEffect3.sprite.xscale = 1.2
				BoneEffect4.sprite.xscale = 1.2
				BoneEffect1.sprite.alpha = 0.8
				BoneEffect2.sprite.alpha = 0.6
				BoneEffect3.sprite.alpha = 0.4
				BoneEffect4.sprite.alpha = 0.2
				Bone.SetVar("MoreKR", 7.5)
				BoneEffect1.SetVar("MoreKR", 7.5)
				BoneEffect2.SetVar("MoreKR", 7.5)
				BoneEffect3.SetVar("MoreKR", 7.5)
				BoneEffect4.SetVar("MoreKR", 7.5)
				Bone.SetVar("MoreDamage", 4)
				BoneEffect1.SetVar("MoreDamage", 4)
				BoneEffect2.SetVar("MoreDamage", 4)
				BoneEffect3.SetVar("MoreDamage", 4)
				BoneEffect4.SetVar("MoreDamage", 4)
				if Bone.GetVar("tipo1") == "Blue" then
					Bone.sprite.color = {33/255, 197/255, 1}
				elseif Bone.GetVar("tipo1") == "Orange" then
					Bone.sprite.color = {1, 150/255, 0}
				end
				Bone.SetVar("tipo", Bone.GetVar("tipo1"))
				BoneEffect1.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect2.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect3.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect4.SetVar("tipo", Bone.GetVar("tipo"))
			elseif timer >= 261 and timer < 270 then
				if Bone.GetVar("tipo1") == "Blue" then
					Bone.sprite.color = {Bone.sprite.color[1] + 222/255/9, Bone.sprite.color[2] + 58/255/9, 1}
				elseif Bone.GetVar("tipo1") == "Orange" then
					Bone.sprite.color = {1, Bone.sprite.color[2] + 105/255/9, Bone.sprite.color[3] + 1/9}
				end
			elseif timer >= 278 and timer < 287 then
				if Bone.GetVar("tipo2") == "Blue" then
					Bone.sprite.color = {Bone.sprite.color[1] - 222/255/9, Bone.sprite.color[2] - 58/255/9, 1}
				elseif Bone.GetVar("tipo2") == "Orange" then
					Bone.sprite.color = {1, Bone.sprite.color[2] - 105/255/9, Bone.sprite.color[3] - 1/9}
				end
				Bone.SetVar("tipo", Bone.GetVar("tipo2"))
				BoneEffect1.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect2.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect3.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect4.SetVar("tipo", Bone.GetVar("tipo"))
			elseif timer >= 296 and timer < 305 then
				if Bone.GetVar("tipo2") == "Blue" then
					Bone.sprite.color = {Bone.sprite.color[1] + 222/255/9, Bone.sprite.color[2] + 58/255/9, 1}
				elseif Bone.GetVar("tipo2") == "Orange" then
					Bone.sprite.color = {1, Bone.sprite.color[2] + 105/255/9, Bone.sprite.color[3] + 1/9}
				end
			elseif timer >= 314 and timer < 323 then
				if Bone.GetVar("tipo3") == "Blue" then
					Bone.sprite.color = {Bone.sprite.color[1] - 222/255/9, Bone.sprite.color[2] - 58/255/9, 1}
				elseif Bone.GetVar("tipo3") == "Orange" then
					Bone.sprite.color = {1, Bone.sprite.color[2] - 105/255/9, Bone.sprite.color[3] - 1/9}
				end
				Bone.SetVar("tipo", Bone.GetVar("tipo3"))
				BoneEffect1.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect2.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect3.SetVar("tipo", Bone.GetVar("tipo"))
				BoneEffect4.SetVar("tipo", Bone.GetVar("tipo"))
			elseif timer >= 332 and timer < 341 then
				if Bone.GetVar("tipo3") == "Blue" then
					Bone.sprite.color = {Bone.sprite.color[1] + 222/255/9, Bone.sprite.color[2] + 58/255/9, 1}
				elseif Bone.GetVar("tipo3") == "Orange" then
					Bone.sprite.color = {1, Bone.sprite.color[2] + 105/255/9, Bone.sprite.color[3] + 1/9}
				end
			end
			BoneEffect1.sprite.color = Bone.sprite.color
			BoneEffect2.sprite.color = Bone.sprite.color
			BoneEffect3.sprite.color = Bone.sprite.color
			BoneEffect4.sprite.color = Bone.sprite.color
			Bone.sprite.rotation = Bone.sprite.rotation + 10
			BoneEffect1.sprite.rotation = Bone.sprite.rotation - 2
			BoneEffect2.sprite.rotation = Bone.sprite.rotation - 4
			BoneEffect3.sprite.rotation = Bone.sprite.rotation - 6
			BoneEffect4.sprite.rotation = Bone.sprite.rotation - 8
			Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation)))
			BoneEffect1.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 2)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 2)))
			BoneEffect2.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 4)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 4)))
			BoneEffect3.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 6)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 6)))
			BoneEffect4.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 8)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 8)))
		elseif timer >= 350 and timer < 359 then
			if timer == 350 and not timerEnd then
				Body = CreateProjectileAbs("Bullets/Bones/Body", 710, Arena.y + 5 + Arena.height/2)
				Body.sprite.rotation = -32
				Body.sprite.yscale = 114
				Body.sprite.color = {1, 150/255, 0}
				Body.SetVar("X", 710)
				Body.SetVar("Y", Arena.y + 5 + Arena.height/2)
				Body.SetVar("Limit", 320)
				Body.SetVar("tipo", "Orange")
				Body.SetVar("velrot", 4)
				Body.SetVar("angdiff", 0)
				table.insert(Bones, Body)
				Body = CreateProjectileAbs("Bullets/Bones/Body", 710, Arena.y + 5 + Arena.height/2)
				Body.sprite.rotation = 58
				Body.sprite.yscale = 114
				Body.sprite.color = {1, 150/255, 0}
				Body.SetVar("X", 710)
				Body.SetVar("Y", Arena.y + 5 + Arena.height/2)
				Body.SetVar("Limit", 320)
				Body.SetVar("tipo", "Orange")
				Body.SetVar("velrot", 4)
				Body.SetVar("angdiff", 90)
				table.insert(Bones, Body)
			end
			if Bone.GetVar("tipo4") == "Blue" then
				Bone.sprite.color = {Bone.sprite.color[1] - 222/255/9, Bone.sprite.color[2] - 58/255/9, 1}
			elseif Bone.GetVar("tipo4") == "Orange" then
				Bone.sprite.color = {1, Bone.sprite.color[2] - 105/255/9, Bone.sprite.color[3] - 1/9}
			end
			BoneEffect1.sprite.color = Bone.sprite.color
			BoneEffect2.sprite.color = Bone.sprite.color
			BoneEffect3.sprite.color = Bone.sprite.color
			BoneEffect4.sprite.color = Bone.sprite.color
			Bone.sprite.rotation = 164 + 90*math.sin(math.rad(10*(timer - 350)))
			BoneEffect1.sprite.rotation = Bone.sprite.rotation - 2
			BoneEffect2.sprite.rotation = Bone.sprite.rotation - 4
			BoneEffect3.sprite.rotation = Bone.sprite.rotation - 6
			BoneEffect4.sprite.rotation = Bone.sprite.rotation - 8
			Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation)))
			BoneEffect1.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 2)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 2)))
			BoneEffect2.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 4)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 4)))
			BoneEffect3.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 6)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 6)))
			BoneEffect4.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation - 8)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation - 8)))
		elseif timer >= 359 and timer < 368 then
			if BoneEffect4 then
				BoneEffect1.sprite.alpha = 0.6
				BoneEffect2.sprite.alpha = 0.4
				BoneEffect3.sprite.alpha = 0.2
				BoneEffect4.Remove()
				BoneEffect4 = nil
			elseif BoneEffect3 then
				BoneEffect1.sprite.alpha = 0.4
				BoneEffect2.sprite.alpha = 0.2
				BoneEffect3.Remove()
				BoneEffect3 = nil
			elseif BoneEffect2 then
				BoneEffect1.sprite.alpha = 0.2
				BoneEffect2.Remove()
				BoneEffect2 = nil
			elseif BoneEffect1 then
				BoneEffect1.Remove()
				BoneEffect1 = nil
			end
		elseif timer >= 368 and timer < 377 then
			if Bone.GetVar("tipo4") == "Blue" then
				Bone.sprite.color = {Bone.sprite.color[1] + 222/255/9, Bone.sprite.color[2] + 58/255/9, 1}
			elseif Bone.GetVar("tipo4") == "Orange" then
				Bone.sprite.color = {1, Bone.sprite.color[2] + 105/255/9, Bone.sprite.color[3] + 1/9}
			end
		elseif timer == 377 and timerEnd then
			StopWave = true
		elseif timer == 538 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.SetVar("velrot", 0)
					bullet.SetVar("tipo", nil)
					bullet.sprite.color = {1, 1, 1}
				end
			end
		elseif timer >= 568 and timer < 578 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.sprite.rotation = bullet.GetVar("angdiff") - 10*math.sin(math.rad(9*(timer - 567)))
				end
			end
		elseif timer >= 578 and timer < 618 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.sprite.rotation = bullet.GetVar("angdiff") - 10 + 90*math.sin(math.rad(2.25*(timer - 577)))
				end
			end
		elseif timer >= 628 and timer < 638 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.sprite.rotation = bullet.GetVar("angdiff") + 80 - 10*math.sin(math.rad(9*(timer - 627)))
				end
			end
		elseif timer >= 638 and timer < 678 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.sprite.rotation = bullet.GetVar("angdiff") + 70 + 110*math.sin(math.rad(2.25*(timer - 637)))
				end
			end
		elseif timer == 688 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.SetVar("tipo", "Blue")
					bullet.sprite.color = {33/255, 197/255, 1}
				end
			end
		elseif timer == 698 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.SetVar("velrot", 2.5)
				end
			end
		elseif timer == 710 and not timerEnd then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 0}, DatosFinales = {X = 455, Y = Player.absy, rotation = -90}}, Delay = 70, Duration = 40})
			Audio.PlaySound("gasterintro")
		elseif timer == 860 and not timerEnd then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 0}, DatosFinales = {X = 455, Y = Player.absy, rotation = -90}}, Delay = 70, Duration = 40})
			Audio.PlaySound("gasterintro")
		elseif timer == 1010 and not timerEnd then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 0}, DatosFinales = {X = 455, Y = Player.absy, rotation = -90}}, Delay = 70, Duration = 40})
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","Top"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 0}, DatosFinales = {X = Player.absx, Y = 294, rotation = 0}}, Delay = 70, Duration = 40})
			Audio.PlaySound("gasterintro")
			Audio.PlaySound("gasterintro2")
		elseif timer == 1160 then
			for i=1,#Bones do
				local bullet = Bones[i]
				if bullet then
					bullet.SetVar("Limit", -70)
				end
			end
		elseif timer == 1260 then
			Encounter.Call("ResetAnim", true)
			EndWave()
		end
	elseif #GBPaps <= 0 then
		if timerEnd < 180 then
			timerEnd = timerEnd + 1
			if timerEnd == 10 then
				Encounter["GameMusic"] = "Fade"
			end
		else
			Encounter.Call("ResetAnim", true)
			EndWave()
		end
	end
	shake = nil
	Arena.MoveTo(320, 90, false, true)
	HandL.MoveToAbs(276, 348)
	if timer > 400 then
		Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)), 346 + 114*math.sin(math.rad(Bone.sprite.rotation)))
	end
	Encounter.Call("PapsMoveTo", {320, 285})
	Encounter.Call("StatusMoveTo", {320, 70.5})
	Encounter.Call("ButtonMoveTo", {"Fight", 87, 27})
	Encounter.Call("ButtonMoveTo", {"Act", 240, 27})
	Encounter.Call("ButtonMoveTo", {"Item", 400, 27})
	Encounter.Call("ButtonMoveTo", {"Mercy", 555, 27})
	for i=1,#GBPaps do
		local gb = GBPaps[i - tableaux]
		if not gb.X then
			gb.X = gb.Sprite.x
		end
		if not gb.Y then
			gb.Y = gb.Sprite.y
		end
		gb.Sprite.xscale = (gb.Scale or 1)
		if gb.Animacion then
			if not gb.Animacion.timer then
				gb.Animacion.timer = 0
			end
			gb.Animacion.timer = gb.Animacion.timer + 1
			gb.X = gb.Animacion.DatosIniciales.X + (gb.Animacion.DatosFinales.X - gb.Animacion.DatosIniciales.X)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			gb.Y = gb.Animacion.DatosIniciales.Y + (gb.Animacion.DatosFinales.Y - gb.Animacion.DatosIniciales.Y)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			gb.Sprite.MoveTo(gb.X, gb.Y)
			while gb.Animacion.DatosIniciales.rotation >= 360 or gb.Animacion.DatosIniciales.rotation < 0 do
				if gb.Animacion.DatosIniciales.rotation < 0 then
					gb.Animacion.DatosIniciales.rotation = gb.Animacion.DatosIniciales.rotation + 360
				else
					gb.Animacion.DatosIniciales.rotation = gb.Animacion.DatosIniciales.rotation - 360
				end
			end
			while gb.Animacion.DatosFinales.rotation >= 360 or gb.Animacion.DatosFinales.rotation < 0 do
				if gb.Animacion.DatosFinales.rotation < 0 then
					gb.Animacion.DatosFinales.rotation = gb.Animacion.DatosFinales.rotation + 360
				else
					gb.Animacion.DatosFinales.rotation = gb.Animacion.DatosFinales.rotation - 360
				end
			end
			if gb.Animacion.DatosFinales.rotation - gb.Animacion.DatosIniciales.rotation <= 180 and gb.Animacion.DatosFinales.rotation - gb.Animacion.DatosIniciales.rotation >= -180 then
				gb.Sprite.rotation = gb.Animacion.DatosIniciales.rotation - (gb.Animacion.DatosIniciales.rotation - gb.Animacion.DatosFinales.rotation)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			else
				gb.Sprite.rotation = gb.Animacion.DatosIniciales.rotation - (360 - (gb.Animacion.DatosFinales.rotation - 180 + gb.Animacion.DatosIniciales.rotation - 180)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30)))))
			end
			if gb.Animacion.timer >= (gb.Animacion.VelAppearing or 30) then
				gb.Animacion = nil
				gb.Done = true
			end
		else
			if gb.Rotation and not gb.Done then
				gb.Sprite.rotation = gb.Rotation
			end
			gb.Delay = gb.Delay - 1
		end
		if gb.Vel then
			gb.X = gb.X - gb.Vel*math.sin(math.rad(gb.Sprite.rotation))
			gb.Y = gb.Y + gb.Vel*math.cos(math.rad(gb.Sprite.rotation))
			gb.Vel = gb.Vel*1.1
			if gb.X < -100 or gb.X > 740 or gb.Y < -100 or gb.Y > 580 then
				gb.Vel = 0
			end
		end
		gb.Sprite.MoveTo(gb.X, gb.Y)
		if gb.Delay < 0 then
			if gb.Delay == -1 then
				gb.Sprite.Set("Bullets/GB Papyrus/1")
				gb.Vel = 2
				Audio.PlaySound("gasterfire")
			elseif gb.Delay <= -2 then
				if gb.Duration > 0 then
					gb.Duration = gb.Duration - 1
					if not gb.Laser then
						gb.Laser = {Misil = CreateProjectileAbs("Bullets/GB Laser", gb.X + (640 + 26*gb.Sprite.yscale)*math.sin(math.rad(gb.Sprite.rotation)), gb.Y - (640 + 26*gb.Sprite.yscale)*math.cos(math.rad(gb.Sprite.rotation)))}
						gb.Laser.Misil.sprite.xscale = 0
						gb.Laser.Misil.sprite.rotation = gb.Sprite.rotation
						gb.Laser.Scale = 0
						if gb.Sprite.layer == "Top" then
							gb.Laser.Misil.sprite.layer = "Top"
						end
					else
						gb.Laser.Misil.MoveToAbs(gb.X + (640 + 26*gb.Sprite.yscale)*math.sin(math.rad(gb.Sprite.rotation)), gb.Y - (640 + 26*gb.Sprite.yscale)*math.cos(math.rad(gb.Sprite.rotation)))
						if gb.Laser.Misil.sprite.xscale < 0.9*gb.Sprite.xscale and gb.Laser.Scale == 0 then
							gb.Laser.Misil.sprite.xscale = gb.Laser.Misil.sprite.xscale + 0.1*gb.Sprite.xscale
						else
							gb.Laser.Scale = gb.Laser.Scale + 50
							gb.Laser.Misil.sprite.xscale = 0.9*gb.Sprite.xscale + 0.1*gb.Sprite.xscale*math.sin(math.rad(gb.Laser.Scale))
						end
						if not shake then
							shake = {math.random(-4, 4), math.random(-4, 4)}
							Arena.MoveTo(320 + shake[1], 90 + shake[2], false, true)
							HandL.MoveToAbs(276 + shake[1], 348 + shake[2])
							Bone.MoveToAbs(276 + 114*math.cos(math.rad(Bone.sprite.rotation)) + shake[1], 346 + 114*math.sin(math.rad(Bone.sprite.rotation)) + shake[2])
							Encounter.Call("PapsMoveTo", {320 + shake[1], 285 + shake[2]})
							Encounter.Call("StatusMoveTo", {320 + shake[1], 70.5 + shake[2]})
							Encounter.Call("ButtonMoveTo", {"Fight", 87 + shake[1], 27 + shake[2]})
							Encounter.Call("ButtonMoveTo", {"Act", 240 + shake[1], 27 + shake[2]})
							Encounter.Call("ButtonMoveTo", {"Item", 400 + shake[1], 27 + shake[2]})
							Encounter.Call("ButtonMoveTo", {"Mercy", 555 + shake[1], 27 + shake[2]})
						end
					end
				else
					gb.Laser.Misil.SetVar("No Damage", true)
					if gb.Laser.Misil.sprite.xscale > 0 then
						gb.Laser.Misil.MoveToAbs(gb.X + (640 + 26*gb.Sprite.yscale)*math.sin(math.rad(gb.Sprite.rotation)), gb.Y - (640 + 26*gb.Sprite.yscale)*math.cos(math.rad(gb.Sprite.rotation)))
						gb.Laser.Misil.sprite.xscale = gb.Laser.Misil.sprite.xscale - 0.05*gb.Sprite.xscale
						gb.Laser.Misil.sprite.alpha = gb.Laser.Misil.sprite.xscale
					else
						gb.Laser.Misil.Remove()
						gb.Sprite.Remove()
						gb = nil
						table.remove(GBPaps, i - tableaux)
						tableaux = tableaux + 1
					end
				end
			end
		end
		if gb and shake then
			gb.Sprite.MoveTo(gb.X + shake[1], gb.Y + shake[2])
			if gb.Laser then
				gb.Laser.Misil.Move(shake[1], shake[2])
			end
		end
	end
	tableaux = 0
	for i=1,#Bones do
		local bullet = Bones[i]
		if not bullet.GetVar("head") then
			bullet.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", bullet.absx - (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.absy + (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation))))
		end
		if not bullet.GetVar("legs") then
			bullet.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", bullet.absx + (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.absy - (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation))))
		end
		if timerEnd and #GBPaps <= 0 then
			bullet.SetVar("Limit", -70)
			bullet.SetVar("velrot", 2.5)
			bullet.SetVar("tipo", "Gray")
			bullet.sprite.color = {0.5, 0.5, 0.5}
			StopWave = true
		end
		bullet.sprite.rotation = bullet.sprite.rotation + bullet.GetVar("velrot")
		bullet.GetVar("head").sprite.rotation = bullet.sprite.rotation
		bullet.GetVar("legs").sprite.rotation = bullet.sprite.rotation + 180
		bullet.GetVar("head").sprite.color = bullet.sprite.color
		bullet.GetVar("legs").sprite.color = bullet.sprite.color
		if bullet.sprite.rotation >= 0 and bullet.sprite.rotation < 45 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(bullet.sprite.rotation))
		elseif bullet.sprite.rotation >= 45 and bullet.sprite.rotation < 90 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(45 - (bullet.sprite.rotation - 45)))
		elseif bullet.sprite.rotation >= 90 and bullet.sprite.rotation < 135 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(bullet.sprite.rotation - 90))
		elseif bullet.sprite.rotation >= 135 and bullet.sprite.rotation < 180 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(45 - (bullet.sprite.rotation - 135)))
		elseif bullet.sprite.rotation >= 180 and bullet.sprite.rotation < 225 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(bullet.sprite.rotation - 180))
		elseif bullet.sprite.rotation >= 225 and bullet.sprite.rotation < 270 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(45 - (bullet.sprite.rotation - 225)))
		elseif bullet.sprite.rotation >= 270 and bullet.sprite.rotation < 315 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(bullet.sprite.rotation - 270))
		elseif bullet.sprite.rotation >= 315 and bullet.sprite.rotation < 360 then
			bullet.sprite.yscale = 114 + 50*math.tan(math.rad(45 - (bullet.sprite.rotation - 315)))
		end
		bullet.SetVar("X", math.max(bullet.GetVar("Limit"), bullet.GetVar("X") - 4))
		bullet.GetVar("head").SetVar("tipo", bullet.GetVar("tipo"))
		bullet.GetVar("legs").SetVar("tipo", bullet.GetVar("tipo"))
		if shake then
			bullet.MoveToAbs(bullet.GetVar("X") + shake[1], bullet.GetVar("Y") + shake[2])
			bullet.GetVar("head").MoveToAbs(bullet.GetVar("X") + shake[1] - (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.GetVar("Y") + shake[2] + (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation)))
			bullet.GetVar("legs").MoveToAbs(bullet.GetVar("X") + shake[1] + (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.GetVar("Y") + shake[2] - (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation)))
		else
			bullet.MoveToAbs(bullet.GetVar("X"), bullet.GetVar("Y"))
			bullet.GetVar("head").MoveToAbs(bullet.GetVar("X") - (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.GetVar("Y") + (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation)))
			bullet.GetVar("legs").MoveToAbs(bullet.GetVar("X") + (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.GetVar("Y") - (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation)))
		end
	end
	if state == 0 then
		if Input.Right > 0 then
			newpX = newpX + 2
		end
		if Input.Left > 0 then
			newpX = newpX - 2
		end
		if Input.Up > 0 then
			newpY = newpY + 2
		end
		if Input.Down > 0 then
			newpY = newpY - 2
		end
		if newpX < 328 - Arena.width/2 then
			newpX = 328 - Arena.width/2
		elseif newpX > 312 + Arena.width/2 then
			newpX = 312 + Arena.width/2
		end
		if newpY < 103 then
			newpY = 103
		elseif newpY > 87 + Arena.height then
			newpY = 87 + Arena.height
		end
		if shake then
			Player.MoveToAbs(newpX + shake[1], newpY + shake[2], true)
		else
			Player.MoveToAbs(newpX, newpY, true)
		end
    end
end

function OnHit(bullet)
	if not bullet.GetVar("No Damage") then
		if bullet.GetVar("tipo") then
			if bullet.GetVar("tipo") == "Blue" and (Input.Down > 0 or Input.Up > 0 or Input.Left > 0 or Input.Right > 0) and not Player.isHurting then
				if Player.hp == 1 then
					Player.Hurt(0, 2)
					Encounter["GameOver"] = true
					timerEnd = 0
				end
				if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
					if bullet.GetVar("MoreDamage") and bullet.GetVar("MoreDamage") < Player.hp - 1 then
						Player.hp = Player.hp - bullet.GetVar("MoreDamage")
					elseif not bullet.GetVar("MoreDamage") and Player.hp > 1 then
						Player.hp = Player.hp - 1
					else
						Player.hp = 1
					end
					Player.Hurt(0, 2/60)
				elseif Player.hp > 1 then
					Player.Hurt(0, 2/60)
					Encounter.Call("AddKR", 0.5*(Danio + 1) + (bullet.GetVar("MoreKR") or 0))
				end
				Encounter["NOHIT"] = nil
			elseif bullet.GetVar("tipo") == "Orange" and (Input.Down <= 0 and Input.Up <= 0 and Input.Left <= 0 and Input.Right <= 0) and not Player.isHurting then
				if Player.hp == 1 then
					Player.Hurt(0, 2)
					Encounter["GameOver"] = true
					timerEnd = 0
				end
				if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
					if bullet.GetVar("MoreDamage") and bullet.GetVar("MoreDamage") < Player.hp - 1 then
						Player.hp = Player.hp - bullet.GetVar("MoreDamage")
					elseif not bullet.GetVar("MoreDamage") and Player.hp > 1 then
						Player.hp = Player.hp - 1
					else
						Player.hp = 1
					end
					Player.Hurt(0, 2/60)
				elseif Player.hp > 1 then
					Player.Hurt(0, 2/60)
					Encounter.Call("AddKR", 0.5*(Danio + 1) + (bullet.GetVar("MoreKR") or 0))
				end
				Encounter["NOHIT"] = nil
			end
		elseif not Player.isHurting then
			if Player.hp == 1 then
				Player.Hurt(0, 2)
				Encounter["GameOver"] = true
				timerEnd = 0
			end
			if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
				if bullet.GetVar("MoreDamage") and bullet.GetVar("MoreDamage") < Player.hp - 1 then
					Player.hp = Player.hp - bullet.GetVar("MoreDamage")
				elseif not bullet.GetVar("MoreDamage") and Player.hp > 1 then
					Player.hp = Player.hp - 1
				else
					Player.hp = 1
				end
				Player.Hurt(0, 2/60)
			elseif Player.hp > 1 then
				Player.Hurt(0, 2/60)
				Encounter.Call("AddKR", 0.5*(Danio + 1))
			end
			Encounter["NOHIT"] = nil
		end
	end
end